﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using DevComponents.DotNetBar.SuperGrid;
using Einstein.Data;
using Einstein.Utility;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Basic
{
    public partial class Basic_UserType : Office2007Form
    {
        public Basic_UserType()
        {
            InitializeComponent();
            BindUserType();
        }

        void BindUserType()
        {
            UserTypeInfoDAL dal = new UserTypeInfoDAL();
            dgList.PrimaryGrid.DataSource = dal.SelectAll();
            Basic_UserType_Edit.Enabled = false;
            Basic_UserType_Delete.Enabled = false;
        }

        private void Basic_UserType_New_Click(object sender, EventArgs e)
        {
            Basic_UserType_New usertype_new = new Basic_UserType_New();
            usertype_new.Text = "新建用户类型";
            if (usertype_new.ShowDialog() == DialogResult.OK)
            {
                BindUserType();
            }
        }

        private void Basic_UserType_Edit_Click(object sender, EventArgs e)
        {
            if (CurrentData != null)
            {
                Basic_UserType_New usertype_new = new Basic_UserType_New();
                usertype_new.Text = "编辑用户类型";
                usertype_new.UserTypeInfo = CurrentData;
                usertype_new.BindData();
                if (usertype_new.ShowDialog() == DialogResult.OK)
                {
                    BindUserType();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        } 


        UserTypeInfo currentData;
        public UserTypeInfo CurrentData
        {
            get
            {
                return currentData;
            }
            set
            {
                Basic_UserType_Edit.Enabled = true;
                Basic_UserType_Delete.Enabled = true;
                currentData = value;
            }
        }

        private void dgList_RowClick(object sender, GridRowClickEventArgs e)
        {
            UserTypeInfo currentItem = ((DevComponents.DotNetBar.SuperGrid.GridRow)e.GridRow).DataItem as UserTypeInfo;
            if (currentItem != null)
            {
                CurrentData = currentItem;
            }
        }

        private void Basic_UserType_Delete_Click(object sender, EventArgs e)
        {
            if (BoxManager.ShowDeleteAsk() != DialogResult.OK)
            {
                return;
            }

            if (CurrentData != null)
            {
                UserTypeInfoDAL dal = new UserTypeInfoDAL();
                bool result = dal.Delete(new UserTypeInfo { UserTypeID = CurrentData.UserTypeID });
                if (result)
                {
                    BindUserType();
                    BoxManager.ShowDeleteSuccess();
                }
                else
                {
                    BoxManager.ShowDeleteFail();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }

    }
}
